var app = getApp();
var md5 = require('../../../libs/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files: [],
    full:0,
    empty:5,
    pingjia:0,
    orderid:'',
    shopid:'',
    content:'',
    type:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options);
      this.setData({
        orderid:options.id,
        shopid:options.shopid,
      })
      if(options.type){
        this.setData({
          type:options.type
        })
      }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 评分
  pingfen:function(e){
    var inx = e.currentTarget.dataset.in;
    var full;
    if (inx == 'full') {
      full = Number(e.currentTarget.id)
    } else {
      full = Number(e.currentTarget.id) + this.data.full
    }
    this.setData({
      full: full,
      empty: 5 - full
    })

    if(this.data.content != ''){
        this.setData({
          pingjia:1
        })
    }


  },
  
  // 添加图片
  chooseImage: function (e) {
    var that = this;
    var images = that.data.files;
    var timestamp = Date.parse(new Date()) / 1000;
    var random = Math.floor(Math.random() * 100);
    var sign = 'GHRDY65' + String(random) + String(timestamp);
    sign = md5.hexMD5(sign);
    wx.chooseImage({
      count:6 - images.length,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res);
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          wx.uploadFile({
            url: app.static.url + 'api/common/uploadFile',
            filePath: res.tempFilePaths[i],
            name: 'file',
            formData: {
              timestamp: timestamp,
              random: random,
              sign: sign
            },
            success: function (ret) {
              console.log(ret)
              var data = ret.data;
              data = JSON.parse(data);
              if (data.code == 1) {
                that.setData({
                  files: that.data.files.concat(data.data.url)
                });
              }
            }
          })
        }
      }
    })
  },
  // 预览图片
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id,
      urls: this.data.files
    })
  },
  //长按图片删除
  deleteImage: function (e) {
    var that = this;
    var images = that.data.files;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    wx.showModal({
      title: '提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  },
  // 图片删除效果
  closeClick: function (e) {
    var that = this;
    var images = that.data.files;
    var index = e.currentTarget.dataset.index; //获取当前按图片下标
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })

  },
  // 预览图片
  preview: function (e) {
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.files[index], // 当前显示图片的http链接  
      urls: this.data.files // 需要预览的图片http链接列表  
    })
  },
  cblur:function(e){
    if (e.detail.value != ''){
      this.setData({
        content: e.detail.value,
      })
      if(this.data.full > 0){
        this.setData({
          pingjia:1
        })
      }
    }
    
  },
  tijiao:function(e){
      var content = e.detail.value.content;
      var userid = wx.getStorageSync("userId");
      var that = this;
      if(this.data.full == 0){
          wx.showToast({
            title: '给个评分吧？',
            icon:'none',
            duration:2000
          })
          return false;
      }
    if (!e.detail.value.content && that.data.files.length == 0){
        wx.showToast({
          title: '请填写评价或者上传图片',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      var num = that.data.full;
      num = num.toFixed(1);
      let data = {
        user_id: userid,
        shop_id: that.data.shopid,
        score: num,
        content: content,
        imgs: that.data.files
      }
      let url;
      if(this.data.type == 0){
        url= 'api/user/addEvaluation';
        data.demand_id= that.data.orderid;
      }
      if(this.data.type == 1 ){
        url="api/userr/addOilComment"
        data.order_id= that.data.orderid;
      } 
      if(this.data.type == 2 ){
        url="api/userr/addCarComment"
        data.order_id= that.data.orderid;
      }
      var param = {
        url:url,
        data:data,
        success:function(e){
          wx.showToast({
            title: e.data.msg,
            icon:'none',
            duration:2000
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            })
          },1000)
          
        },
        
      };
      app.post(param);
  },
})